package com.gzh.teaching.controller;

import com.gzh.core.model.vo.Result;
import com.gzh.core.service.CheckToken;
import com.gzh.core.util.RequestContextUtil;
import com.gzh.teaching.model.vo.*;
import com.gzh.teaching.service.TeacherService;
import com.gzh.teaching.service.dto.CourseQueryOut;
import com.gzh.teaching.service.dto.GetQuestionOut;
import com.gzh.teaching.service.dto.PublishWorkOut;
import com.gzh.teaching.service.dto.SaveJobOut;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.util.List;

@Slf4j
@RequestMapping("/v1/api/ter")
@RestController
public class TeacherController {
  @Autowired
  private TeacherService teacherService;

  @PostMapping("/addCourse")
  @CheckToken
  public ResponseEntity<?> addCourse(@Valid  @RequestBody  AddCourseParam param) {
    Integer userId = RequestContextUtil.getCurrentUserId();
    teacherService.addCourse(param.paramToIn(userId));
    return Result.ok();
  }

  @GetMapping("/getCourseList")
  @CheckToken
  public ResponseEntity<?> getCourseList(CourseQueryParam param) {
    Integer userId = RequestContextUtil.getCurrentUserId();
    List<CourseQueryOut> list =teacherService.getCourseList(param.paramToIn(userId));
    return Result.ok(list);
  }

  @GetMapping("/getOptionalCourse")
  @CheckToken
  public ResponseEntity<?> getOptionalCourse(CourseQueryParam param) {
    Integer userId = RequestContextUtil.getCurrentUserId();
    List<CourseQueryOut> list =teacherService.getOptionalCourse(param.paramToIn(userId));
    return Result.ok(list);
  }

  @PostMapping("/saveJob")
  @CheckToken
  public ResponseEntity<?> saveJob(@RequestBody  SaveJobParam param) {
    Integer userId = RequestContextUtil.getCurrentUserId();
    SaveJobOut out =teacherService.saveJob(param.paramToIn(userId));
    return Result.ok(out);
  }

  @PostMapping("/saveReport")
  @CheckToken
  public ResponseEntity<?> saveReport(@RequestBody  SaveJobParam param) {
    Integer userId = RequestContextUtil.getCurrentUserId();
    SaveJobOut out =teacherService.saveReport(param.paramToIn(userId));
    return Result.ok(out);
  }

  @GetMapping("/getJob")
  @CheckToken
  public ResponseEntity<?> getJob(GetJobParam param) {
    Integer userId = RequestContextUtil.getCurrentUserId();
    List<GetJobOut> list =teacherService.getJob(param.paramToIn(userId));
    return Result.ok(list);
  }

  @GetMapping("/getQuestion")
  @CheckToken
  public ResponseEntity<?> getQuestion(GetJobParam param) {
    Integer userId = RequestContextUtil.getCurrentUserId();
    GetQuestionOut out = teacherService.getQuestion(param.paramToIn(userId));
    return Result.ok(out);
  }

  @PostMapping("/publishWork")
  @CheckToken
  public ResponseEntity<?> publishWork(@RequestBody PublishWorkParam param) {
    Integer userId = RequestContextUtil.getCurrentUserId();
    PublishWorkOut out =teacherService.publishWork(param.paramToIn(userId));
    return Result.ok(out);
  }

  @PostMapping("/editEndWork")
  @CheckToken
  public ResponseEntity<?> editEndWork(@RequestBody PublishWorkParam param) {
    Integer userId = RequestContextUtil.getCurrentUserId();
    PublishWorkOut out =teacherService.editEndWork(param.paramToIn(userId));
    return Result.ok(out);
  }

  @PostMapping("/deleteWork")
  @CheckToken
  public ResponseEntity<?> deleteWork(@RequestBody PublishWorkParam param) {
    Integer userId = RequestContextUtil.getCurrentUserId();
    PublishWorkOut out =teacherService.deleteWork(param.paramToIn(userId));
    return Result.ok(out);
  }

  @GetMapping("/queryUser")
  @CheckToken
  public ResponseEntity<?> queryUser(QueryUserParam param) {
    Integer userId = RequestContextUtil.getCurrentUserId();
    return Result.ok(teacherService.queryUser(param.paramToIn(userId)));
  }

  @GetMapping("/queryCourseUser")
  @CheckToken
  public ResponseEntity<?> queryCourseUser(CourseUserParam param) {
    Integer currentUserId = RequestContextUtil.getCurrentUserId();
    return Result.ok(teacherService.queryCourseUser(param.paramToIn(currentUserId)));
  }

  @PostMapping("/deleteCourseUser")
  @CheckToken
  public ResponseEntity<?> deleteCourseUser(@RequestBody  CourseUserParam param) {
    Integer currentUserId = RequestContextUtil.getCurrentUserId();
    teacherService.deleteCourseUser(param.paramToIn(currentUserId));
    return Result.ok();
  }

  @PostMapping("/addUser")
  @CheckToken
  public ResponseEntity<?> addUser(@RequestBody AddUserParam param ) {
    Integer userId = RequestContextUtil.getCurrentUserId();
    return Result.ok(teacherService.addUser(param.paramToIn(userId)));
  }

  @PostMapping("/editCourse")
  @CheckToken
  public ResponseEntity<?> editCourse(@RequestBody EditCourseParam param ) {
    Integer userId = RequestContextUtil.getCurrentUserId();
    return Result.ok(teacherService.editCourse(param.paramToIn(userId)));
  }

  @GetMapping("/queryWorkDone")
  @CheckToken
  public ResponseEntity<?> queryWorkDone(GetJobParam param) {
    Integer userId = RequestContextUtil.getCurrentUserId();
    return Result.ok(teacherService.queryWorkDone(param.paramToIn(userId)));
  }

  @PostMapping("/saveComment")
  @CheckToken
  public ResponseEntity<?> saveComment(@RequestBody SaveCommentParam param ) {
    Integer userId = RequestContextUtil.getCurrentUserId();
    teacherService.saveComment(param.paramToIn(userId));
    return Result.ok();
  }

  @PostMapping("/initiateCheckIn")
  @CheckToken
  public ResponseEntity<?> InitiateCheckIn(@RequestBody @Valid CheckInParam param) {
    Integer userId = RequestContextUtil.getCurrentUserId();
    teacherService.initiateCheckIn(param);
    return Result.ok();
  }

  @GetMapping("/queryCheck")
  @CheckToken
  public ResponseEntity<?> queryCheck(@Valid  QueryCheckParam param) {
    return Result.ok(teacherService.queryCheck(param));
  }

  @GetMapping("/exportPuntaje")
//  @CheckToken
  public void exportPuntaje(QueryCheckParam param, HttpServletResponse response) {
    teacherService.exportPuntaje(param.getCourseId(),response);
  }
}
